/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.attachment;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import twaver.AlarmSeverity;
import twaver.ComponentCallbackHandler;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;

public class AlarmAttachment extends ComponentAttachment{

	private ButtonGroup group;
	private JPanel panel = TWaverUtil.createVerticalPanel(0);
	
	public AlarmAttachment(String name, ElementUI ui) {
		super(name, ui);
	
		JLabel label = new JLabel("Select Alarm", JLabel.CENTER);
		this.panel.add(label);
		this.group = new ButtonGroup();
		this.createButton(AlarmSeverity.CRITICAL);
		this.createButton(AlarmSeverity.MAJOR);
		this.createButton(AlarmSeverity.MINOR);
		this.createButton(AlarmSeverity.WARNING);
		this.createButton(AlarmSeverity.INDETERMINATE);
        
		this.setMinimizable(true);
        this.setSize(this.panel.getPreferredSize());
        this.setPosition(TWaverConst.POSITION_BOTTOM);
        this.setYOffset(5);
        
        TWaverUtil.iteratorComponent(panel, new ComponentCallbackHandler(){
			public void processComponent(Component component) {
				if(component instanceof JComponent){
					((JComponent)component).setOpaque(false);
				}
			}
        });
        
		label.setBackground(Color.BLACK);
		label.setForeground(Color.WHITE);
		label.setOpaque(true);
        
        panel.setOpaque(true);
        panel.setBackground(new Color(255, 255, 255, 160));
        
        this.setMinimizedIcon(TWaverUtil.getImageIcon("/demo/network/topo/attachment/alarm.png"));       
		this.setComponent(panel);
	}
	
	private JRadioButton createButton(AlarmSeverity severity){
		ImageIcon icon = TWaverUtil.createBallIcon(13, 13, Color.WHITE, severity.getColor(), Color.BLACK);
		JRadioButton button = new JRadioButton(severity.getName());
		this.group.add(button);
		JPanel p = new JPanel(new BorderLayout());
		p.setBorder(BorderFactory.createEmptyBorder(0,4,0,10));
		p.add(button, BorderLayout.CENTER);
		p.add(new JLabel(icon), BorderLayout.EAST);
		this.panel.add(p);
		return button;
	}

	public void performAction(int gesture, MouseEvent e) {
		if(TWaverConst.MOUSE_LEFT_CLICKED == gesture && !this.isShrinked() && !this.isMinimized()){
			Component comp = getComponentAt(e);
			if(comp instanceof JRadioButton){
				JRadioButton button = (JRadioButton)comp;
				AlarmSeverity severity = AlarmSeverity.getByName(button.getText());
				element.getAlarmState().clear();
				element.getAlarmState().addNewAlarm(severity);
				button.setSelected(true);
			}
		}
		super.performAction(gesture, e);
	}
}